package com.ly.cyclictest.test02;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Solder implements Runnable {
     private final CyclicBarrier cyclicBarrier;

    private String solder;

    public Solder(CyclicBarrier cyclicBarrier, String solder) {
        this.cyclicBarrier = cyclicBarrier;
        this.solder = solder;
    }


    @Override
    public void run() {
        //士兵执行任务
        try {
            cyclicBarrier.await();
            dowork();
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

     void dowork() {
        try {
            Thread.sleep(Math.abs(new Random().nextInt()%10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(solder+"执行任务");
    }
}
